package com.central.scheduler.api.controller;

import com.central.common.annotation.LoginUser;
import com.central.common.annotation.RequiredPermission;
import com.central.common.constant.PermissionConstants;
import com.central.common.model.*;
import com.central.common.utils.UserRoleUtil;
import com.central.scheduler.api.service.IWorkerGroupService;
import com.central.scheduler.api.service.IWorkerServerService;
import com.central.scheduler.dao.model.WorkerGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Tindy
 * @date 2021/6/24
 * @describe
 */
@RestController
@Api(tags = "worker节点模块api")
@Slf4j
@RequestMapping("/worker")
public class WorkerController {
    @Autowired
    private IWorkerServerService workerServerService;
    @Autowired
    private IWorkerGroupService workerGroupService;

    @ApiOperation(value = "查询worker列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/findWorkers")
    public PageResult<WorkerServer> findWorkers(@ApiIgnore @LoginUser SysUser user, @RequestParam Map<String, Object> params) {
        if (UserRoleUtil.checkSysAdmin(user.getRoles())) {
            return workerServerService.findWorkers(params);
        } else {
            return workerServerService.findWorkersByPro(params,user.getCurrentProjectId());
        }
    }

    @ApiOperation(value = "修改节点名称")
    @GetMapping("/updateName")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result updateName(Long workerId,String name) {
        try {
            WorkerServer worker = workerServerService.getById(workerId);
            worker.setName(name);
            workerServerService.updateById(worker);
            return Result.succeed("操作成功");
        } catch (Exception ex) {
            log.error("scheduler-updateNickname-error", ex);
            return Result.failed("操作失败");
        }
    }

    @ApiOperation(value = "修改节点描述")
    @GetMapping("/updateDesc")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result updateDesc(Long workerId,String desc) {
        try {
            WorkerServer worker = workerServerService.getById(workerId);
            worker.setDescription(desc);
            workerServerService.updateById(worker);
            return Result.succeed("操作成功");
        } catch (Exception ex) {
            log.error("scheduler-updateDesc-error", ex);
            return Result.failed("操作失败");
        }
    }

    @ApiOperation(value = "配置项目")
    @PostMapping("/setPros")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result setPros(Long workerId,@RequestBody Set<Long> proIds) {
        try {
            return workerServerService.setPros(workerId,proIds);
        } catch (Exception ex) {
            log.error("scheduler-setPros-error", ex);
            return Result.failed("操作失败");
        }
    }

    @ApiOperation(value = "获取worker所有项目")
    @GetMapping("/getPros")
    public List<SysProject> getPros(Long workerId) {
        return workerServerService.getPros(workerId);
    }

    @ApiOperation(value = "删除worker节点")
    @DeleteMapping("/delete")
    @RequiredPermission(PermissionConstants.SYS_ADMIN)
    public Result delete(Long workerId) {
        try {
            return workerServerService.removeByIdAndOff(workerId);
        } catch (Exception ex) {
            log.error("scheduler-master-delete-error", ex);
            return Result.failed("操作失败");
        }
    }


    @ApiOperation(value = "创建/更新worker节点组")
    @PostMapping("/addOrUpdateWorkerGroup")
    @RequiredPermission({PermissionConstants.PRO_ADMIN,PermissionConstants.SYS_ADMIN})
    public Result addOrUpdateWorkerGroup(@ApiIgnore @LoginUser SysUser user, @RequestBody WorkerGroup workerGroup) {
        Long proId=null;
        if (UserRoleUtil.checkSysAdmin(user.getRoles())) {
            proId=-1l;
        }else {
            proId=user.getCurrentProjectId();
        }
        try {
            return workerGroupService.addOrUpdateWorkerGroup(proId,workerGroup);
        }catch (Exception ex) {
            log.error("scheduler-createWorkerGroup-error", ex);
            return Result.failed("操作失败");
        }
    }

    @ApiOperation(value = "删除worker节点组")
    @DeleteMapping("/delWorkerGroup")
    @RequiredPermission({PermissionConstants.PRO_ADMIN,PermissionConstants.SYS_ADMIN})
    public Result delWorkerGroup(@ApiIgnore @LoginUser SysUser user, Long id) {
        Long proId=null;
        if (UserRoleUtil.checkSysAdmin(user.getRoles())) {
            proId=-1l;
        }else {
            proId=user.getCurrentProjectId();
        }
        try {
            workerGroupService.delWorkerGroup(id,proId);
            return Result.succeed("删除成功");
        }catch (Exception ex) {
            log.error("scheduler-delWorkerGroup-error", ex);
            return Result.failed("操作失败");
        }
    }

    @ApiOperation(value = "查询worker组列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页起始位置", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页结束位置", required = true, dataType = "Integer")
    })
    @GetMapping("/findWorkerGroups")
    public PageResult<WorkerGroup> findWorkerGroups(@ApiIgnore @LoginUser SysUser user, @RequestParam Map<String, Object> params) {
        if (!UserRoleUtil.checkSysAdmin(user.getRoles())) {
            Long proId=user.getCurrentProjectId();
            params.put("proId",proId);
        }
        return workerGroupService.findWorkerGroupsByPro(params);
    }
}
